package com.bobo.controller.front;

import com.bobo.annotations.RequestMapping;
import com.bobo.controller.base.BaseServlet;
import com.bobo.dto.Page;
import com.bobo.dto.VCTopic;
import com.bobo.model.Category;
import com.bobo.model.Tag;
import com.bobo.model.Topic;
import com.bobo.service.category.CategoryServiceImpl;
import com.bobo.service.category.ICategoryService;
import com.bobo.service.tag.ITagService;
import com.bobo.service.tag.TagServiceImpl;
import com.bobo.service.topic.ITopicService;
import com.bobo.service.topic.TopicServiceImpl;
import com.bobo.service.topicTag.ITopicTagService;
import com.bobo.service.topicTag.TopicTagServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by bobo on 16-10-29.
 */
@WebServlet(name = "index",urlPatterns = {"/index/*"})
public class Index extends BaseServlet {



    @RequestMapping("/blog")
    public String get(HttpServletRequest request){

        ITagService tagService = new TagServiceImpl();
        List<Tag> tags = tagService.getTags(0,0,1);
        request.setAttribute("tags",tags);

        //推荐文章
        ITopicService topicService = new TopicServiceImpl();
        List<Topic> topics = topicService.getTops(1);
        request.setAttribute("topics",topics);

//        //置顶文章、
//        List<Topic> tops = topicService.getTops(2);
//        request.setAttribute("tops",tops);

        int current = 1;
        int pageSize = 5;
        int categoryId;
        int tagId;
        String title;
        Page<Topic> page;


        if(request.getParameter("current") != null) {
            current = Integer.parseInt(request.getParameter("current"));
        }

        if(request.getParameter("pageSize") != null) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }

        if(request.getParameter("categoryId")!=null){
            categoryId = Integer.parseInt(request.getParameter("categoryId"));
            page = topicService.getTopicsByCategoryId(categoryId, current, pageSize, 1);

        }else {
            if (request.getParameter("tagId") != null) {
                tagId = Integer.parseInt(request.getParameter("tagId"));
                ITopicTagService topicTagService = new TopicTagServiceImpl();
                page = topicTagService.getTopics(tagId, current, pageSize);

            } else {
                if (request.getParameter("title") != null && !"".equals(request.getParameter("title").trim())) {
                    title = request.getParameter("title").replace("%","");
                    page = topicService.getLike(current,pageSize,title);

                }else {
                    page = topicService.getTopics(current, pageSize, 1);
                }
            }
        }
        request.setAttribute("page",page);
        return "jsp/front/index";
    }
}
